<template>
  <el-config-provider :locale="zhCn">
    <div v-if="showLayout" class="app-container">
      <!-- 侧边栏 -->
      <el-aside width="200px" class="aside">
        <div class="logo">
          <h2>端到端安全可靠传输系统</h2>
        </div>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical"
          :router="true"
          :collapse="isCollapse"
        >
          <el-menu-item index="/home">
            <el-icon><HomeFilled /></el-icon>
            <template #title>个人首页</template>
          </el-menu-item>
          <el-menu-item index="/chat">
            <el-icon><ChatDotRound /></el-icon>
            <template #title>加密对话</template>
          </el-menu-item>
          <el-menu-item index="/files">
            <el-icon><Folder /></el-icon>
            <template #title>文件传输</template>
          </el-menu-item>
          <el-menu-item index="/rules">
            <el-icon><Document /></el-icon>
            <template #title>规则管理</template>
          </el-menu-item>
          <el-menu-item index="/settings">
            <el-icon><Setting /></el-icon>
            <template #title>系统设置</template>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主要内容区 -->
      <el-container class="main-container">
        <!-- 顶部栏 -->
        <el-header class="header">
          <div class="header-left">
            <el-button
              type="text"
              @click="toggleCollapse"
            >
              <el-icon>
                <component :is="isCollapse ? 'Expand' : 'Fold'" />
              </el-icon>
            </el-button>
          </div>
          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <span class="user-dropdown">
                {{ username }}
                <el-icon><CaretBottom /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="settings">设置</el-dropdown-item>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>

        <!-- 内容区 -->
        <el-main class="main">
          <router-view />
        </el-main>
      </el-container>
    </div>
    <router-view v-else />
  </el-config-provider>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import {
  HomeFilled,
  ChatDotRound,
  Folder,
  Setting,
  CaretBottom,
  Expand,
  Fold,
  Document
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const isCollapse = ref(false)
const username = computed(() => localStorage.getItem('username') || '用户')

// 根据路由元信息判断是否显示布局
const showLayout = computed(() => route.meta.requiresAuth)

const activeMenu = computed(() => route.path)

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const handleCommand = (command: string) => {
  switch (command) {
    case 'settings':
      router.push('/settings')
      break
    case 'logout':
      localStorage.removeItem('token')
      localStorage.removeItem('username')
      router.push('/login')
      ElMessage.success('已退出登录')
      break
  }
}
</script>

<style scoped lang="scss">
.app-container {
  height: 100vh;
  display: flex;
  
  .aside {
    background-color: #304156;
    transition: width 0.3s;
    overflow: hidden;
    
    .logo {
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: #2b2f3a;
      
      h2 {
        color: #fff;
        font-size: 16px;
        margin: 0;
        white-space: nowrap;
      }
    }
    
    .el-menu {
      border-right: none;
      background-color: #304156;
      
      :deep(.el-menu-item) {
        color: #bfcbd9;
        transition: all 0.3s ease;
        
        &:hover {
          color: #409EFF;
          background-color: #263445;
          transform: translateX(4px);
        }
        
        &.is-active {
          color: #409EFF;
          background-color: #263445;
          border-right: 3px solid #409EFF;
        }

        .el-icon {
          transition: transform 0.3s ease;
        }

        &:hover .el-icon {
          transform: scale(1.1);
        }
      }
    }
  }
  
  .main-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    
    .header {
      background-color: #fff;
      border-bottom: 1px solid #e6e6e6;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      
      .header-left {
        .el-button {
          font-size: 20px;
        }
      }
      
      .header-right {
        .user-dropdown {
          display: flex;
          align-items: center;
          cursor: pointer;
          color: #606266;
          
          .el-icon {
            margin-left: 5px;
          }
        }
      }
    }
    
    .main {
      background-color: #f0f2f5;
      padding: 20px;
      overflow-y: auto;
    }
  }
}
</style> 